<?php
# 请求者
class Invoker
{
    /**
     * @var CommandInterface
     */
    private $command;
    
    /**
     * @param CommandInterface $cmd
     */
    public function setCommand(CommandInterface $cmd)
    {
        $this->command = $cmd;
    }
    
    /**
     * 执行命令
     */
    public function run()
    {
        $this->command->execute();
    }
}

# 命令接口
interface CommandInterface
{
    public function execute();
}

# 具体命令类
class DoCommand implements CommandInterface
{
    /**
     * @var Receiver
     */
    private $receiver;
    
    
    public function __construct(Receiver $receiver)
    {
        $this->receiver = $receiver;
    }
    
    public function execute()
    {
        $this->receiver->start();
        $this->receiver->write('Hello World');
    }
}

# 执行者
class Receiver
{
    public function start()
    {
        echo 'write start' . PHP_EOL;
    }
    
    /**
     * @param string $str
     */
    public function write(string $str)
    {
        echo $str;
    }
}

# client
Class Client
{
    public function do() {
        
        $receiver = new Receiver();
        
        $command = new DoCommand($receiver);
        
        $invoker = new Invoker();
        $invoker->setCommand($command);
        
        $invoker->run();
    }
}

(new Client())->do();